<template>
  <div id="addPicturePage">
    <h2 style="margin-bottom: 16px">
      {{ route.query?.id ? '修改图片' : '创建图片' }}
    </h2>
<!--    <PictureUpload :picture="picture" :onSuccess="onSuccess" />-->
    <!-- 选择上传方式 -->
    <a-tabs v-model:activeKey="uploadType"
    >>
      <a-tab-pane key="file" tab="文件上传">
        <PictureUpload :picture="picture" :onSuccess="onSuccess" />
      </a-tab-pane>
      <a-tab-pane key="url" tab="URL 上传" force-render>
        <UrlPictureUpload :picture="picture" :onSuccess="onSuccess" />
      </a-tab-pane>
    </a-tabs>
    <div v-if="picture" class="edit-bar">
      <a-button :icon="h(EditOutlined)" @click="doEditPicture">编辑图片</a-button>
      <ImageCropper
        ref="imageCropperRef"
        :imageUrl="picture?.url"
        :picture="picture"
        :spaceId="spaceId"
        :onSuccess="onCropSuccess"
      />
    </div>
    <a-form v-if="picture" layout="vertical" :model="pictureForm" @finish="handleSubmit">
      <a-form-item label="名称" name="name">
<!--普通输入框-->
        <a-input v-model:value="pictureForm.name" placeholder="请输入名称" />
      </a-form-item>
      <a-form-item label="简介" name="introduction">
<!--        多行输入框-->
        <a-textarea
          v-model:value="pictureForm.introduction"
          placeholder="请输入简介"
          :rows="2"
          autoSize
          allowClear
        />
      </a-form-item>
      <a-form-item label="分类" name="category">
<!--自由输入，同时给出提示        -->
        <a-auto-complete
          v-model:value="pictureForm.category"
          placeholder="请输入分类"
          allowClear
          :options="categoryOptions"
        />
      </a-form-item>
      <a-form-item label="标签" name="tags">
<!--Select选择框，根据mode：tags 自由输入和多选        -->
        <a-select
          v-model:value="pictureForm.tags"
          mode="tags"
          :options="tagsOptions"
          placeholder="请输入标签"
          allowClear
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">创建</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script lang="ts" setup>
import PictureUpload from '@/components/pictureUpload/PictureUpload.vue'
import ImageCropper from '@/components/modal/ImageCropper.vue'
import UrlPictureUpload from '@/components/pictureUpload/UrlPictureUpload.vue'
const uploadType = ref<'file' | 'url'>('file')

import { onMounted, reactive, ref,h } from 'vue'
import {
  editPictureUsingPost,
  getPictureVoUsingGet,
  listPictureTagCategoryUsingGet
} from '@/api/pictureController'
import { message } from 'ant-design-vue'
import { useRoute, useRouter } from 'vue-router'
import { EditOutlined } from '@ant-design/icons-vue'
//图片Vo
const picture = ref<API.PictureVO>()
//上传成功
const onSuccess = (newPicture: API.PictureVO) => {
  picture.value = newPicture
  pictureForm.name = newPicture.name
}
const router = useRouter()
//图片表单
const pictureForm = reactive<API.PictureEditDTO>({})
//图片表单提交
const handleSubmit = async (values:any) => {
  const pictureId = picture.value?.id
  //查看图片id是否存在
  console.log(pictureId)
  if(!pictureId){
    //不存在则直接返回
    return
  }
  const res = await editPictureUsingPost({
    id: pictureId,
    ...values,
  })
  if(res.data.code === 0 && res.data.data ) {
    message.success('创建成功')
    //跳转到图片详情页面
    router.push({
      path:`/picture/${pictureId}`,
    })
  }else{
    //创建失败
    message.error('创建失败'+res.data.msg)
  }

}
//类型选项
const categoryOptions = ref<string[]>([])
//tags选项
const tagsOptions = ref<string[]>([])
const getTagCategoryOptions = async ()=>{
  //根据接口获取tags and categories
  const res = await listPictureTagCategoryUsingGet()
  if(res.data.code === 0 && res.data.data ) {
    //将其转化成下拉选项组件接受的格式
    tagsOptions.value = (res.data.data.tagList ?? []).map((data:string)=>{
      return {
        value : data,
        label: data,
      }
    })
    categoryOptions.value = (res.data.data.categoryList ?? []).map((data:string)=>{
      return {
        value : data,
        label: data,
      }
    })
  }
  else{
    //失败
    message.error('加载选项失败'+res.data.msg)
  }
}
const route = useRoute()
// 获取老数据
const getOldPicture = async () => {
  // 获取数据
  const id = route.query?.id
  if (id) {
    const res = await getPictureVoUsingGet({
      id: id,
    })
    if (res.data.code === 0 && res.data.data) {
      const data = res.data.data
      picture.value = data
      pictureForm.name = data.name
      pictureForm.introduction = data.introduction
      pictureForm.category = data.category
      pictureForm.tags = data.tags
    }
  }
}
onMounted(() => {
  getTagCategoryOptions()
  getOldPicture()

})
const spaceId = ref(route.query?.spaceId)

// 图片编辑弹窗引用
const imageCropperRef = ref()

// 编辑图片
const doEditPicture = () => {
  if (imageCropperRef.value) {
    imageCropperRef.value.openModal()
  }
}

// 编辑成功事件
const onCropSuccess = (newPicture: API.PictureVO) => {
  picture.value = newPicture
}



</script>

<style scoped>
#addPicturePage {
  max-width: 720px;
  margin: 0 auto;
}
#addPicturePage .edit-bar {
  text-align: center;
  margin: 16px 0;
}

</style>
